const fs = require('fs')
const webpack = require("webpack")
const {RawSource} = webpack.sources
const path = require('path')
const util = require("util")
const fsread = util.promisify(fs.readFile)

class plugins2{
    apply(compiler){
        //初始化compilation钩子
        compiler.hooks.thisCompilation.tap("plugins2",(compilation)=>{
            //添加钩子
            compilation.hooks.addtionalAssets.tapAsync('plugins2',async (cb)=>{

                const content = 'hello plugin2'
                //往要输出的资源中加入a.txt
                compilation.assets['a.txt'] = {
                    //文件大小
                    size(){
                        return content.length
                    },
                    //文件内容
                    source(){
                        return content 
                    }
                }

                const data = await fsread(path.resolve(__dirname,'b.txt'))
                compilation.assets['b.txt'] = new RawSource(data)
                cb()
            })
        })
    }
}

module.exports = plugins2;